#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    unsigned int i = 0; //1轮次
    unsigned int j = 0; //2轮次
    void* p[1024] = {NULL};
    unsigned int c = 0; //已分配个数
    unsigned int r = 0; //随机值
    srand(time(NULL));
    while(1)
    {
        r = rand()%1024;
        if(rand()%2) //分配
        {
            if(p[c] != NULL) free(p[c]); //删除之前分配的
            p[c] = malloc(10*r+1); //分配一个
            if(p[c] == NULL) goto EXIT; //内存碎片最大化产生
            c++;
            if(c >= 1024) c = 0;

            //成功分配一次
            i++;
            if(i == 0)
            {
                j++;
                printf("loop %d\n", j);
            }
        }
        else //释放
        {
            if(p[r] != NULL)
            {
                free(p[r]);
                p[r] = NULL;
            }
        }
    }
    EXIT:
    printf("loop count=%d * (UINT32_MAX) + %d\n", j, i);
    return 0;
}
